package com.cabinet.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.service.IDiauthorityService;

import com.cabinet.web.entity.Diauthority;
import com.cabinet.web.entity.Seller;
import com.cabinet.web.entity.User;

import cn.com.cabinet.dao.DiauthorityDao;
import cn.com.cabinet.dao.UserDao;
import cn.com.cabinet.service.SellerServiceImpl;
import cn.com.easy.utils.CookieUtils;

@Controller
@RequestMapping("/seller/relenisment")
public class RelenishmentController {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private DiauthorityDao diauthorityDao;
	
	@Autowired
	private SellerServiceImpl sellerServiceImpl;
	
	@Autowired
	private IDiauthorityService iDiauthorityService;
	
	@RequestMapping("/index")
	public String index(){
		return "/replenishmentClerkManager/replenishment";
		
	}
	
	@RequestMapping("/getuser")
	@ResponseBody
	public List<User> getUser(String key){
		List<User> uList = userDao.getUserListByuName(key);
		for (User user : uList) {
			System.out.println(user.getOpenId());
		}
		return uList;
		
	}
	
	@RequestMapping("/myRelenishment")
	public String getMyRelenishment(HttpServletRequest request,ModelMap modelMap) throws Exception{
		String pfCount = CookieUtils.getCookieValue(request, "pfCount");
		Seller s = sellerServiceImpl.getSellerBysAccount(pfCount);
		List<Diauthority> dList= iDiauthorityService.getDauthorityListBySellerId(s.getId());
		List<User> uList = new ArrayList<>();

		for (Diauthority diauthority : dList) {
			User u = userDao.findOne(diauthority.getUserId());
			uList.add(u);
		}
		modelMap.addAttribute("uList", uList);
		return "/replenishmentClerkManager/myreplenishment";
	}

	
	@RequestMapping("/add")
	@ResponseBody
	public String addRelenishment(HttpServletRequest request,Long id) throws Exception{
		String pfCount = CookieUtils.getCookieValue(request, "pfCount");
		
		
			Seller s = sellerServiceImpl.getSellerBysAccount(pfCount);
			Diauthority d = iDiauthorityService.getDauthorityBySellerIdAndUserId(s.getId(), id);
			if(d == null){
				Diauthority di= diauthorityDao.getDauthorityByUserId(id);
				if(di == null){
					Diauthority dia = new Diauthority();
					dia.setSellerId(s.getId());
					dia.setUserId(id);
					iDiauthorityService.addDiauthority(dia);
					return "添加成功";
				}else{
					return "该用户已是补货员，您无法添加他为您的补货员";
				
				}
			}else{
				return "您已添加该用户为补货员";
			}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public String deleteRelenishment(HttpServletRequest request,Long id) throws Exception{
		String pfCount = CookieUtils.getCookieValue(request, "pfCount");
		Seller s = sellerServiceImpl.getSellerBysAccount(pfCount);
		Diauthority d = iDiauthorityService.getDauthorityBySellerIdAndUserId(s.getId(), id);
		iDiauthorityService.deleteDiauthority(d);
		return "删除成功";
	}
	
}
